import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class demo1 {
    class Solution {
        public List<Integer> findAnagrams(String s, String p) {
            char[]ss=s.toCharArray();
            char[]pp=p.toCharArray();
            HashMap<Character,Integer>hashMap1=new HashMap<>();
            HashMap<Character,Integer> hashMap2=new HashMap<>();
            for(int i=0;i<p.length();i++){
                hashMap2.put(pp[i],hashMap2.getOrDefault(pp[i],0)+1);
            }
            int n=s.length();
            int left=0,right=0;
            int count=0;
            List<Integer>list=new ArrayList<>();
            while (right<n){
                hashMap1.put(ss[right],hashMap1.getOrDefault(ss[right],0)+1);
                if(hashMap2.containsKey(ss[right])){
                    if(hashMap1.get(ss[right]).equals(hashMap2.getOrDefault(ss[right],0)))count++;
                    while (count==hashMap2.size()){
                        list.add(left);
                        count--;
                        hashMap1.put(ss[left],hashMap1.getOrDefault(ss[left],0)-1);
                    }
                }else{
                    left=right;
                    count=0;
                }
                right++;
            }
            return list;
        }
    }
}
